/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.dao.student;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.uwm.universitydb.dao.SearchMode;
import edu.uwm.universitydb.model.Student;

/**
 * <code>SearchStudentsCommand</type> 
 * Command class for searching students
 * 
 * @author Shyam Nair
 */
public class SearchStudentsByNameCommand extends SearchStudentCommandBase<List<Student>>
{
    /** sql portion representing where clause for name */
    private static final String SQL_WHERE_CLAUSE_NAME = " where name like ?";

    /** sql portion for order by clause */
    private static final String SQL_ORDER_BY = " order by name";

    /** attribute holding the name */
    private String mName;

    /** attribute holding the search mode */
    private final SearchMode mMode;

    /**
     * 
     */
    public SearchStudentsByNameCommand()
    {
        mMode = SearchMode.SEARCH_ALL;
    }

    /**
     * 
     * @param name
     */
    public SearchStudentsByNameCommand(final String name)
    {
        mMode = SearchMode.SEARCH_BY_NAME;
        mName = name;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DataFetchCommand#processResult(java.sql.ResultSet)
     */
    @Override
    public List<Student> processResult(final ResultSet result) throws SQLException
    {
        final List<Student> studentList = new ArrayList<Student>();
        while (result.next())
        {
            final Student professor = getStudent(result);
            studentList.add(professor);
        }
        return studentList;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DBCommand#getSQL()
     */
    @Override
    public String getSQL()
    {
        if (SearchMode.SEARCH_BY_NAME.equals(mMode))
        {
            return SELECT_SQL + SQL_WHERE_CLAUSE_NAME + SQL_ORDER_BY;
        }
        else
        {
            return SELECT_SQL + SQL_ORDER_BY;
        }
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DBCommand#setParameters(java.sql.PreparedStatement)
     */
    @Override
    public void setParameters(final PreparedStatement pStmt) throws SQLException
    {
        if (SearchMode.SEARCH_BY_NAME.equals(mMode))
        {
            pStmt.setString(1, "%" + mName + "%");
        }
    }

}
/*
 * ============================================================================
 * Change History
 * ----------------------------------------------------------------------------
 * Apr 19, 2014   Shyam Nair
 * File created
 * ----------------------------------------------------------------------------
 */